﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;
using System.Drawing;
using System.Diagnostics;

namespace BiffenGUI
{
    public class SeatTableHelper
    {

        public DataGridView Table { get; set; }
        public List<Booking> Bookings { get; private set; }
        List<int> selectedSeats = new List<int>();

        public int Rows { get; private set; }
        public int Cols { get; private set; }

        public SeatTableHelper(DataGridView table, int rows, int cols)
        {
            Table = table;
            Rows = rows;
            Cols = cols;

            Table.ColumnCount = Cols;

            Table.Rows.Add(rows);

            int seatno = 0;

            // set row height
            int height = table.Height - table.ColumnHeadersHeight - 4;
            int rowHeight = (height / rows);

            for (int row = 0; row < Rows; row++)
            {
                Table.Rows[row].Height = rowHeight;
                Table.Rows[row].MinimumHeight = rowHeight;
                for (int col = 0; col < Cols; col++)
                {
                    seatno++;
                    //Table[col, row]
                    Table[col, row].Value = seatno.ToString();
                    if (Cols == 8 && seatno == 8)
                    {
                        seatno = 0;
                    }
                    else if (Cols == 10 && seatno == 10)
                    {
                        seatno = 0;
                    }
                    Table.Columns[col].HeaderCell.Value = (col + 1).ToString();
                    Table.Columns[col].SortMode = DataGridViewColumnSortMode.NotSortable;
                    Table.Rows[row].HeaderCell.Value = (row + 1).ToString();

                }
            }

        }

        private void ResetColors()
        {
            for (int row = 0; row < Rows; row++)
            {
                for (int col = 0; col < Cols; col++)
                {
                    Table[col, row].Style.BackColor = Table.DefaultCellStyle.BackColor;
                }
            }
        }

        public void SetBookings(List<Booking> bookings)
        {

            ResetColors();

            Bookings = new List<Booking>(bookings);

            foreach (Booking booking in Bookings)
            {
                foreach (int seat in booking.Seats)
                {

                    int row = GetSeatRow(seat);
                    int col = GetSeatCol(seat);

                    Color color;

                    switch (booking.Status)
                    {
                        case Booking.States.Reserved:
                            color = Color.Yellow;
                            break;

                        case Booking.States.Sold:
                            color = Color.Red;
                            break;

                        case Booking.States.Released:
                            color = Color.LightBlue;
                            break;

                        default:
                            color = Color.White;
                            break;
                    }
                    

                    Table[col, row].Style.BackColor = color;

                }
                
            }

        }

        private int GetSeatRow(int seat)
        {
            int row = 0;

            if(seat > Cols) {
                row = (seat - 1) / Cols; // subtract one to compensate for 0-based table
            }

            return row;
        }

        private int GetSeatCol(int seat)
        {
            int col = 0;

            col = (seat - 1) % Cols; // subtract one to compensate for 0-based table

            return col;
        }

        public void ToggleSeat(int row, int col)
        {
            if (row >= 0 && col >= 0)
            {
                int value = 0;
                value = int.Parse(Table[col, row].Value.ToString()) + (row * Cols);

                // if the seat is already selected, deselect it
                if (selectedSeats.Contains(value))
                {
                    Table[col, row].Style.BackColor = Table.DefaultCellStyle.BackColor;
                    selectedSeats.Remove(value);
                    Debug.WriteLine(DateTime.Now.Ticks.ToString() + ": Removed " + row + "x" + col);
                }
                else
                {
                    // check if the seat is available before doing anything
                    if(IsAvailable(value)) {
                        Table[col, row].Style.BackColor = Color.LightGray;
                        selectedSeats.Add(value);
                        Debug.WriteLine(DateTime.Now.Ticks.ToString() + ": Added " + row + "x" + col);
                    }
                    
                }

                Table.Update();
            }
        }

        /// <summary>
        /// Checks if a given seat is available for reservation
        /// </summary>
        /// <param name="seatId"></param>
        /// <returns></returns>
        public bool IsAvailable(int seatId)
        {

            foreach (var booking in Bookings)
            {
                foreach (var seat in booking.Seats)
                {
                    if(seat.Equals(seatId)) {

                        if (booking.Status != Booking.States.Released)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        public List<int> GetSelectedSeats()
        {
            return selectedSeats;
        }

    }
}
